<template>
	<el-container>
		<el-aside width="240px">
			<el-container>
				<el-main class="nopadding">
					<blCategoryTree title="活动分类" ref="group" model="ActivityV2" @node-click="groupClick" />
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
				<div class="left-panel" style="gap:15px;">
					<el-button v-if="false" type="primary" icon="el-icon-plus" @click="add">00</el-button>
					<el-date-picker v-model="search.dateRange" type="daterange" align="right" unlink-panels
						range-separator="至" start-placeholder="申请时间起始" end-placeholder="申请时间截止"
						value-format="YYYY-MM-DD">
					</el-date-picker>
					<div class="right-panel-search">
						<el-input style="width: 220px;" v-model="search.keyword" placeholder="单据编号/姓名/电话/活动名称"
							clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch">查询</el-button>
					</div>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">

					</div>
				</div>
			</el-header>
			<div>
				<el-card shadow="never">
					<sc-select-filter :data="selectFilter" :selected-values="selectedValues" :label-width="80"
						@on-change="stateChange"></sc-select-filter>
				</el-card>
			</div>
			<el-main class="nopadding">
				<scTable :params="{selectFilter:selectedValues}" ref="table" :apiObj="apiObj.list"
					@selection-change="selectionChange" stripe remoteSort remoteFilter>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="单据编号" prop="wdrl_no" width="200">
						<template #default="scope">
							<span @dblclick="table_edit(scope.row, scope.$index)"> {{scope.row.wdrl_no.toUpperCase()}}
							</span>
						</template>
					</el-table-column>
					<el-table-column label="申请人姓名" prop="wdrl_user_name" width="120"></el-table-column>
					<el-table-column label="申请人手机号" prop="wdrl_phone" width="100"></el-table-column>
					<el-table-column label="申请金额(元)" prop="wdrl_amount" width="100"></el-table-column>
					<el-table-column label="申请/处理时间" width="150">
						<template #default="{row}">
							<p>{{row.wdrl_apply_at}}</p>
							<p v-if="row.wdrl_processed_at">{{row.wdrl_processed_at}}</p>
						</template>
					</el-table-column>
					<el-table-column label="支付方式" prop="wdrl_payment_method" width="80">
					</el-table-column>
					<el-table-column label="审核状态" prop="wdrl_status_text" width="100" align="center">
					</el-table-column>
					<el-table-column label="转账状态" prop="wdrl_state_text" width="120" align="left">
					</el-table-column>
					<el-table-column label="创建时间" prop="created_at">
					</el-table-column>
					<el-table-column label="操作" fixed="right" align="right" width="280">
						<template #default="scope">
							<template v-if="scope.row.wdrl_status=='approved'">
								<el-button-group>
									<el-popconfirm title="确定要发放吗？" @confirm="wxpayTransfer(scope.row, scope.$index)">
										<template #reference>
											<el-button text type="primary" size="small">微信发放</el-button>
										</template>
									</el-popconfirm>
									<el-button @click="wxpayQuery(scope.row, scope.$index)" text type="primary"
										size="small">查询付款单</el-button>
									<el-popconfirm title="取消付款单？" @confirm="wxpayCancel(scope.row, scope.$index)">
										<template #reference>
											<el-button text type="primary" size="small">取消付款单</el-button>
										</template>
									</el-popconfirm>
								</el-button-group>
							</template>
							<template v-if="scope.row.wdrl_status=='pending'">
								<el-button-group>
									<el-popconfirm title="通过审核？" @confirm="wdrlRecordApprove(scope.row, scope.$index)">
										<template #reference>
											<el-button text type="primary" size="small">通过</el-button>
										</template>
									</el-popconfirm>
									<el-button @click="showRejectDialog(scope.row, scope.$index)" text type="primary"
										size="small">拒绝</el-button>
								</el-button-group>
							</template>
							<template v-if="scope.row.wdrl_status=='rejected'">
								{{scope.row.wdrl_status_text}}
							</template>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>

	<!-- 拒绝理由弹窗 -->
	<el-dialog title="拒绝理由" v-model="rejectDialogVisible" width="500">
		<el-form :model="rejectForm" ref="rejectForm">
			<el-form-item label="拒绝理由" prop="reject_reason">
				<el-input type="textarea" :rows="4" v-model="rejectForm.reject_reason" placeholder="请输入拒绝理由"></el-input>
			</el-form-item>
		</el-form>

		<span slot="footer" class="dialog-footer">
			<el-button @click="rejectDialogVisible = false">取 消</el-button>
			<el-button type="primary" :loading="rejectLoading" @click="wdrlRecordReject">确 定</el-button>
		</span>
	</el-dialog>


	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
	import saveDialog from './save'
	import scSelectFilter from '@/components/scSelectFilter'

	export default {
		name: 'commission.withdraw',
		components: {
			saveDialog,
			scSelectFilter
		},
		data() {
			return {
				rejectForm: {
					id: '',
					reject_reason: '',
				},
				rejectDialogVisible: false,
				rejectLoading: false,

				selectFilter: [{
					title: "是否处理",
					key: "wdrl_processed_at",
					options: [{
							label: "全部",
							value: null
						},
						{
							label: "待处理",
							value: "is null",
							icon: "el-icon-bottom-left"
						},
						{
							label: "已处理",
							value: "not null",
							icon: "el-icon-flag"
						}
					]
				}],
				selectedValues: {
					wdrl_processed_at: ["is null"]
				},
				dialog: {
					save: false
				},
				apiObj: this.$API.system.wdrlRecord,
				selection: [],
				search: {
					keyword: null,
					dateRange: null
				}
			}
		},
		methods: {
			showRejectDialog(item) {
				console.error(item);
				this.rejectForm.id = item.id;
				this.rejectForm.reject_reason = '';
				this.rejectDialogVisible = true;
			},
			wdrlRecordReject() {
				this.$API.system.wdrlRecord.reject.post(this.rejectForm).then(res => {
					this.$alert(res.msg, "提示", {
						type: res.code == 0 ? 'success' : 'error',
					})
					if (res.code == 0) {
						this.rejectDialogVisible = false;
						this.$refs.table.refresh()
					}
				});
			},
			wdrlRecordApprove(item, index) {
				this.$API.system.wdrlRecord.approve.post({
					id: item.id
				}).then(res => {
					this.$alert(res.msg, "提示", {
						type: res.code == 0 ? 'success' : 'error',
					})
					if (res.code == 0) {
						this.$refs.table.refresh()
					}
				});
			},
			wxpayTransfer(item) {
				this.loading = true
				this.$API.system.wdrlRecordWxpay.transfer.post({
					id: item.id
				}).then(res => {
					console.error(res)
				}).finally(() => {
					this.loading = false;
				})
			},
			wxpayCancel(item, index) {
				this.loading = true
				this.$API.system.wdrlRecordWxpay.cancel.post({
					id: item.id
				}).then(res => {
					if (res.code == 0) {
						this.$refs.table.tableData[index].wdrl_state_text = res.data.wdrl_state_text;
						this.$alert(res.data.wdrl_state_text, "提示", {
							type: 'success'
						})
					} else {
						this.$alert(res.msg, "提示", {
							type: 'error'
						})
					}
				}).finally(() => {
					this.loading = false;
				})
			},
			wxpayQuery(item, index) {
				const loading = this.$loading();
				this.$API.system.wdrlRecordWxpay.query.post({
					id: item.id
				}).then(res => {
					if (res.code == 0) {
						this.$refs.table.tableData[index].wdrl_state_text = res.data.wdrl_state_text;
						this.$alert(res.data.wdrl_state_text, "提示", {
							type: 'success'
						})
					} else {
						this.$refs.table.tableData[index].wdrl_state_text = res.msg;
						this.$alert(res.msg, "提示", {
							type: 'error'
						})
					}
				}).finally(() => {
					loading.close();
				})
			},
			stateChange(e) {
				var params = {
					selectFilter: e
				}
				this.$refs.table.upData(params)
			},
			//添加
			add() {
				this.dialog.save = true
				this.$nextTick(() => {
					const checkedNode = this.$refs.group.getCurrentNode();
					this.$refs.saveDialog.open().setData({
						category_id: checkedNode?.id || null
					});
				})
			},
			//编辑
			table_edit(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//查看
			table_show(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show').setData(row)
				})
			},
			//删除
			async table_del(row, index) {
				var reqData = {
					id: row.id
				}
				var res = await apiObj.remove.post(reqData);
				if (res.code == 0) {
					this.$refs.table.tableData.splice(index, 1);
					this.$message.success(res.msg)
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//批量删除
			async batch_del() {
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning'
				}).then(() => {
					const loading = this.$loading();
					this.selection.forEach(item => {
						this.$refs.table.tableData.forEach((itemI, indexI) => {
							if (item.id === itemI.id) {
								this.$refs.table.tableData.splice(indexI, 1)
							}
						})
					})
					loading.close();
					this.$message.success("操作成功")
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection) {
				this.selection = selection;
			},
			//树点击事件
			groupClick(data) {
				var params = {
					category_id: data.id
				}
				this.$refs.table.upData(params)
			},
			//搜索
			upsearch() {
				this.$refs.table.upData(this.search)
			},
			//本地更新数据
			handleSuccess(data, mode) {
				if (mode == 'add') {
					this.$refs.table.refresh()
				} else if (mode == 'edit') {
					this.$refs.table.refresh()
				}
			}
		}
	}
</script>

<style>
</style>
